﻿<%@ Page Title="" Language="C#" MasterPageFile="~/AdminMaster.Master" AutoEventWireup="true" CodeBehind="MemberPage.aspx.cs" Inherits="SonCa.SocialNetwork.Web.Admin.MemberPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        function ShowEditMember(id) {
            $("#row_member_" + id).hide();
            $("#row_member_edit_" + id).show();

            $("#selectLevelMember_" + id + " option:contains('" + $("#levelNameMember_" + id).text() + "')").attr("selected", true);
            $("#selectIsLockMember_" + id + " option:contains('" + $("#islockMember_" + id).text() + "')").attr("selected",true);
        }

        function HideEditMember(id) {
            $("#row_member_" + id).show();
            $("#row_member_edit_" + id).hide();
        }

        function AcceptEditMember(id) {
            var level = $("#selectLevelMember_" + id).val();
            var islock = $("#selectIsLockMember_" + id).val();
            $.ajax({
                type: "post",
                url: "../Services/AjaxService.svc/UpdateMember_AdminPage",
                data: '{"accountID": "' + id + '","levelID": "' + level + '","isLock": "' + islock + '"}',
                contentType: "application/json; charset=utf-8;",
                dataType: "json",
                beforeSend: function () {
                    $("#loading_editmember_" + id).show();
                },
                success: function (response) {
                    if (response.d != "") {
                        $("#levelNameMember_" + id).text($("#selectLevelMember_" + id + " option:selected").text());
                        $("#islockMember_" + id).text(islock);

                        $("#row_member_" + id).show();
                        $("#row_member_edit_" + id).hide();
                        $("#loading_editmember_" + id).hide();
                    }
                    else {
                        $("#loading_editmember_" + id).hide();
                        alert("Cập nhật thất bại,thử lại.");
                    }
                },
                error: function () {
                    $("#loading_editmember_" + id).hide();
                    alert("Có lỗi xảy ra.");
                }
            });
        }

        function DeleteMember(id) {
            if (confirm("Chắc chắn xóa member này ???")) {
                $.ajax({
                    type: "post",
                    url: "../Services/AjaxService.svc/DeleteMember_AdminPage",
                    data: '{"accountID": "' + id + '"}',
                    contentType: "application/json; charset=utf-8;",
                    dataType: "json",
                    beforeSend: function () {
                        $("#loading_member_" + id).show();
                    },
                    success: function (response) {
                        if (response.d != "") {
                            $("#row_member_" + id).remove();
                            $("#row_member_edit_" + id).remove();
                            $("#loading_member_" + id).hide();
                        }
                        else {
                            $("#loading_member_" + id).hide();
                            alert("Xóa thất bại");
                        }
                    },
                    error: function () {
                        $("#loading_member_" + id).hide();
                        alert("Có lỗi xảy ra.");
                    }
                });
            }
        }

        function ResetPasswordMember(id) {
            if (confirm("Chắc chắn reset password ???")) {                
                $.ajax({
                    type: "post",
                    url: "../Services/AjaxService.svc/ResetPasswordMember_AdminPage",
                    data: '{"accountID": "' + id + '","passReset": "123456"}',
                    contentType: "application/json; charset=utf-8;",
                    dataType: "json",
                    beforeSend: function () {
                        $("#loading_member_" + id).show();
                    },
                    success: function (response) {
                        if (response.d != "") {

                            $("#loading_member_" + id).hide();
                            alert("Reset password success.");
                        }
                        else {
                            $("#loading_member_" + id).hide();
                            alert("Reset pass fail");
                        }
                    },
                    error: function () {
                        $("#loading_member_" + id).hide();
                        alert("Có lỗi xảy ra.");
                    }
                });
            }
        }
    </script>
    
    <table width="80%" style="text-align:center;border:1px solid;border-radius:8px">
        <tr>
            <td><asp:Label ID="lblMessage" runat="server" Visible="false"></asp:Label></td>
        </tr>
        <tr style="font-size:16px;font-weight:bold">
            <td>AccountID</td>
            <td>Username</td>
            <td>Fullname</td>
            <td>Email</td>
            <td>LevelID</td>
            <td>Score</td>
            <td>IsLock</td>                
        </tr>
        <asp:Repeater ID="rptMember" runat="server" onitemdatabound="rptMember_ItemDataBound">
            <ItemTemplate>                
                <tr id="row_member_<%#Eval("AccountID") %>">
                    <td><%#Eval("AccountID") %></td>
                    <td><%#Eval("Username") %></td>
                    <td><%#Eval("FullName") %></td>
                    <td><%#Eval("Email") %></td>                    
                    <td id="levelNameMember_<%#Eval("AccountID") %>"><%#Eval("LevelName") %></td>
                    <td><%#Eval("Score") %></td>
                    <td id="islockMember_<%#Eval("AccountID") %>"><%#Eval("IsLock") %></td>
                    <td><img src="../img/update16.png" onclick="ShowEditMember(<%#Eval("AccountID") %>)" style="cursor:pointer" title="Cập nhật" /></td>
                    <td><img src="../img/check_fail.png" onclick="DeleteMember(<%#Eval("AccountID") %>)" style="cursor:pointer" title="Xóa" /></td>
                    <td><img src="../img/key.png" onclick="ResetPasswordMember(<%#Eval("AccountID") %>)" style="cursor:pointer" title="Reset Password" /></td>
                    <td><img src="../img/loading.gif" id="loading_member_<%#Eval("AccountID") %>" style="display:none" /></td>
                </tr>
                <tr id="row_member_edit_<%#Eval("AccountID") %>" style="display:none">
                    <td><%#Eval("AccountID") %></td>
                    <td><%#Eval("Username") %></td>
                    <td><%#Eval("LevelName") %></td>
                    <td><%#Eval("Email") %></td>                    
                    <td>
                        <select id="selectLevelMember_<%#Eval("AccountID") %>">
                            <asp:Repeater ID="rptLevelMember" runat="server">
                                <ItemTemplate>
                                    <option value="<%#Eval("LevelID") %>"><%#Eval("LevelName") %></option>
                                </ItemTemplate>
                            </asp:Repeater>                            
                        </select>
                    </td>
                    <td><%#Eval("Score") %></td>
                    <td>
                        <select id="selectIsLockMember_<%#Eval("AccountID") %>">
                            <option value="True">True</option>
                            <option value="False">False</option>
                        </select>
                    </td>
                    <td><img src="../img/check_ok.png" onclick="AcceptEditMember(<%#Eval("AccountID") %>)" style="cursor:pointer" title="OK" /></td>
                    <td><img src="../img/uploadify-cancel.png" onclick="HideEditMember(<%#Eval("AccountID") %>)" style="cursor:pointer" title="Hủy" /></td>
                    <td><img src="../img/loading.gif" id="loading_editmember_<%#Eval("AccountID") %>" style="display:none" /></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>    
</asp:Content>
